package cn.openea.perpartner.evaluation.web.controller;

import cn.openea.perpartner.common.core.web.controller.BaseController;
import cn.openea.perpartner.common.core.web.domain.AjaxResult;
import cn.openea.perpartner.common.core.web.page.TableDataInfo;
import cn.openea.perpartner.evaluation.web.entity.EvaluationTask;
import cn.openea.perpartner.evaluation.web.entity.vo.EvaluationTaskVo;
import cn.openea.perpartner.evaluation.web.service.IEvaluationTaskService;
import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author rains
 * @since 2023-07-05 14:18
 */
@Api(tags = "品牌推广")
@RestController
@RequestMapping("/evaluation/task")
public class EvaluationTaskController extends BaseController {

    @Resource
    private IEvaluationTaskService evaluationTaskService;

    @ApiOperation(value = "发布测评活动")
    @PostMapping("/publish")
    public AjaxResult publish(@RequestBody EvaluationTask evaluationTask) {
        return this.evaluationTaskService.publish(evaluationTask);
    }

    @ApiOperation("获取任务详情")
    @GetMapping("/info/{id}")
    public AjaxResult info(@PathVariable String id) {
        if (StringUtils.isBlank(id)) {
            return AjaxResult.error("数据异常，id为空");
        }
        return AjaxResult.success("获取任务详情成功！", this.evaluationTaskService.info(id));
    }

    @ApiOperation("根据订单id获取任务详情")
    @GetMapping("/info/byOrderId/{orderId}")
    public AjaxResult getInfoByOrderId(@PathVariable String orderId) {
        if (StringUtils.isBlank(orderId)) {
            return AjaxResult.error("数据异常，订单为空");
        }
        return AjaxResult.success("获取任务详情成功！", this.evaluationTaskService.getInfoByOrderId(orderId));
    }

    @ApiOperation("获取新的id")
    @GetMapping("/getNewId")
    public AjaxResult getNewId() {
        return AjaxResult.success("获取任务详情成功！", this.evaluationTaskService.getNewId());
    }

    @ApiOperation(value = "获取测评任务列表-我发起的/我参与的")
    @PostMapping("/list")
    public TableDataInfo selectList(@RequestBody EvaluationTaskVo vo){
        startPage();
        List<?> list = this.evaluationTaskService.selectList(vo);
        return getDataTable(list);
    }

    @ApiOperation(value = "获取所有测评任务列表")
    @PostMapping("/page/list")
    public TableDataInfo queryAll(@RequestBody EvaluationTaskVo vo){
        startPage();
        List<EvaluationTask> list = this.evaluationTaskService.queryAll(vo);
        return getDataTable(list);
    }

    @ApiOperation(value = "活动作废")
    @PostMapping("/finish")
    public AjaxResult finish(@RequestBody EvaluationTaskVo vo) {
        if(!this.evaluationTaskService.finish(vo)){
            return AjaxResult.error("作废活动失败！");
        }
        return AjaxResult.success("作废活动成功！", evaluationTaskService.getById(vo.getId()));
    }

    @ApiOperation(value = "活动结束报名")
    @PostMapping("/end/join")
    public AjaxResult endJoin(@RequestBody EvaluationTaskVo vo) {
        if(!this.evaluationTaskService.endJoin(vo)){
            return AjaxResult.error("活动结束报名失败！");
        }
        return AjaxResult.success("活动结束报名成功！", evaluationTaskService.getById(vo.getId()));
    }

    @ApiOperation("补全迁移的历史数据")
    @PostMapping("/deal/historical")
    public AjaxResult dealHistoricalData(@RequestBody JSONObject object){
        if("1".equals(object.getString("runningFlag"))){
            if(!this.evaluationTaskService.dealHistoricalData()){
                return AjaxResult.error("补全迁移的历史数据失败！");
            }
        } else {
            return AjaxResult.error("执行标志的参数传输错误，不执行！");
        }
        return AjaxResult.success("补全迁移的历史数据成功！");
    }

}
